package com.jkl.service.drTemplate;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkl.dao.drTemplate.DrTemplateMapper;
import com.jkl.entity.drTemplate.DrTemplate;
import com.jkl.entity.param.BSParam;
import com.jkl.service.drTemplate.inter.DrTemplateServiceInterface;
import com.jkl.service.param.inter.IBSParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class DrTemplateService implements DrTemplateServiceInterface {

	@Autowired
	private DrTemplateMapper DrTemplateMapper;

	@Autowired
	private IBSParamService paramService;
	//查询全部指标
		@Override
		public PageInfo<DrTemplate> selectTableId(DrTemplate drTemplate) {
			PageHelper.startPage(drTemplate.getCurrentPage(),drTemplate.getPageSize());
			List<DrTemplate> drTemplateList = DrTemplateMapper.selectTableId(drTemplate);
			
			if(!CollectionUtils.isEmpty(drTemplateList)) {
				for (int i = 0; i < drTemplateList.size(); i++) {
					final DrTemplate templateItem = drTemplateList.get(i);
					//level link cycle
					//表级别
					BSParam bSParam1=new BSParam();
					bSParam1.setGroupCode("table_level");
					if(org.apache.commons.lang3.StringUtils.isNotBlank(templateItem.getTableLevel())) {
						bSParam1.setParamCode(templateItem.getTableLevel());
						Optional.ofNullable(paramService.queryCode(bSParam1))
								.ifPresent(res -> templateItem.setTableLevel(res.getValueCode()));
					}

					//表类型
					BSParam bSParam2=new BSParam();
					bSParam2.setGroupCode("link");
					if(org.apache.commons.lang3.StringUtils.isNotBlank(templateItem.getTableType())) {
						bSParam2.setParamCode(templateItem.getTableType());
						Optional.ofNullable(paramService.queryCode(bSParam2))
								.ifPresent(res -> templateItem.setTableType(res.getValueCode()));
					}

					//cycle表周期
					BSParam bSParam3=new BSParam();
					bSParam3.setGroupCode("cycle");
					if(org.apache.commons.lang3.StringUtils.isNotBlank(templateItem.getTableCycle())) {
						bSParam3.setParamCode(templateItem.getTableCycle());
						Optional.ofNullable(paramService.queryCode(bSParam3))
								.ifPresent(res -> templateItem.setTableCycle(res.getValueCode()));
					}

					BSParam bsParam4=new BSParam();
					bsParam4.setGroupCode("scope_type");
					if(org.apache.commons.lang3.StringUtils.isNotBlank(templateItem.getScopeType())) {
						bsParam4.setParamCode(templateItem.getScopeType());
						Optional.ofNullable(paramService.queryCode(bsParam4))
								.ifPresent(res -> templateItem.setScopeType(res.getValueCode()));
					}

				}
			}
			
			PageInfo<DrTemplate> pageInfo = new PageInfo<>(drTemplateList);
			return pageInfo;
		}

		//添加指标
		@Override
		public int save(DrTemplate drTemplate) {
			
			int i = DrTemplateMapper.insert(drTemplate);
			if(i!=0) {
				return i;
			}else {
				return 0;
			}
		}
		@Override
		public int updateDrindex(DrTemplate drTemplate) {
			int i = DrTemplateMapper.updateByPrimaryKey(drTemplate);
			if(i!=0) {
				return i;
			}else {
				return 0;
			}
		}
	
}
